using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace IWCommunity.Blogs.WebControls
{
    public class BlogSettings
    {
        protected SPWeb _currentWeb;

        public void Update()
        {
            WebProperties.Update();
        }

        protected SPPropertyBag WebProperties
        {
            get
            {
                if (_currentWeb == null) _currentWeb = SPContext.Current.Web;
                return _currentWeb.Properties;
            }
        }

        protected bool BoolValue(string key)
        {
            bool bResult;

            if (bool.TryParse(WebProperties[key], out bResult))
                return bResult;

            return true;
        }

        protected int IntValue(string key)
        {
            int iResult;

            if (int.TryParse(WebProperties[key], out iResult))
                return iResult;

            return 0;
        }

        public bool CachingEnabled
        {
            get { return BoolValue("_cksBlogCachingEnabled");}
            set { WebProperties["_cksBlogCachingEnabled"] = value.ToString(); }
        }

        public bool PingbacksEnabled
        {
            get { return BoolValue("_cksBlogPingbacksEnabled"); }
            set { WebProperties["_cksBlogPingbacksEnabled"] = value.ToString(); }
        }

        public int PageOutputCacheTimeout
        {
            get { return IntValue("_cksBlogPageOutputCacheTimeout"); }
            set { WebProperties["_cksBlogPageOutputCacheTimeout"] = value.ToString(); }
        }

        public string PageOutputCacheVaryByParam
        {
            get { return WebProperties["_cksBlogPageOutputCacheVaryByParam"]; }
            set { WebProperties["_cksBlogPageOutputCacheVaryByParam"] = value.ToString(); }
        }

        public bool BlogExtensionsEnabled
        {
            get { return BoolValue("_cksBlogExtensionsEnabled"); }
            set { WebProperties["_cksBlogExtensionsEnabled"] = value.ToString(); }
        }

        public bool DeleteSpamComments
        {
            get { return BoolValue("_cksBlogDeleteSpamComments"); }
            set { WebProperties["_cksBlogDeleteSpamComments"] = value.ToString(); }
        }

        public bool DisableConvertClientDates
        {
            get { return BoolValue("_cksBlogPreventConvertClientDates"); }
            set { WebProperties["_cksBlogPreventConvertClientDates"] = value.ToString(); }
        }

        public bool EnableCaptchaComments
        {
            get { return BoolValue("_cksBlogEnableCaptchaComments"); }
            set { WebProperties["_cksBlogEnableCaptchaComments"] = value.ToString(); }
        }

        public bool TrimPostBodyOnLists 
        {
            get { return BoolValue("_cksBlogTrimPostBodyOnLists"); }
            set { WebProperties["_cksBlogTrimPostBodyOnLists"] = value.ToString(); }
        }

        public bool TrimPostBodyOnRss
        {
            get { return BoolValue("_cksBlogTrimPostBodyOnRss"); }
            set { WebProperties["_cksBlogTrimPostBodyOnRss"] = value.ToString(); }
        }

        public string AkismetKey
        {
            get { return WebProperties["_cksAkismetKey"]; }
            set { WebProperties["_cksAkismetKey"] = value; }
        }

        public string ProxyPort
        {
            get { return WebProperties["_cksProxyPort"]; }
            set { WebProperties["_cksProxyPort"] = value; }
        }
        
        public string ProxyUrl
        {
            get { return WebProperties["_cksProxyUrl"]; }
            set { WebProperties["_cksProxyUrl3"] = value; }
        }

        public string BlogThemeVariation
        {
            get { return WebProperties["_cksBlogThemeVariation"]; }
            set { WebProperties["_cksBlogThemeVariation"] = value; }
        }

        public string BlogTheme
        {
            get { return WebProperties["_cksBlogTheme"]; }
            set { WebProperties["_cksBlogTheme"] = value; }
        }

        public string BlogTransformParameter1
        {
            get { return WebProperties["_cksBlogTransformParameter1"]; }
            set { WebProperties["_cksBlogTransformParameter1"] = value; }
        }

        public string BlogTransformParameter2
        {
            get { return WebProperties["_cksBlogTransformParameter2"]; }
            set { WebProperties["_cksBlogTransformParameter2"] = value; }
        }

        public string BlogTransformParameter3
        {
            get { return WebProperties["_cksBlogTransformParameter3"]; }
            set { WebProperties["_cksBlogTransformParameter3"] = value; }
        }

        public string CustomRSSFeedUrl
        {
            get { return WebProperties["_cksBlogCustomRSSFeedUrl"]; }
            set { WebProperties["_cksBlogCustomRSSFeedUrl"] = value; }
        }
        
        public bool SecureSystemPages
        {
            get { return BoolValue("_cksSecureSystemPages"); }
            set { WebProperties["_cksSecureSystemPages"] = value.ToString(); }
        }

        public bool RedirectPostAspx
        {
            get { return BoolValue("_cksRedirectPostAspx"); }
            set { WebProperties["_cksRedirectPostAspx"] = value.ToString(); }
        }

        public bool ThemeSelectorEnabled
        {
            get { return BoolValue("_cksThemeSelectorEnabled"); }
            set { WebProperties["_cksThemeSelectorEnabled"] = value.ToString(); }
        }

        public string ContactEmail
        {
            get { return WebProperties["_cksBlogContactEmail"]; }
            set { WebProperties["_cksBlogContactEmail"] = value; }
        }

        public string RssItemCount
        {
            get { return WebProperties["_cksBlogRssItemCount"]; }
            set { WebProperties["_cksBlogRssItemCount"] = value; }
        }

        public bool AutoApproveComments
        {
            get { return BoolValue("_cksBlogAutoApproveComments"); }
            set { WebProperties["_cksBlogAutoApproveComments"] = value.ToString(); }
        }
    }
}
